package cn.tedu.car.backend.testShenHe.controller;

import cn.tedu.car.backend.testShenHe.shenHeBO.testSuperNodeActionBO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @Author
 * @Function
 * @Version: 1.0
 * @date 2024/9/12
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class testController {
    private final CompletableFuture<Void> completionFuture = new CompletableFuture<>();
    @PostMapping("/receive-form-data")
    public ResponseEntity<String> receiveFormData(@RequestBody Map<String, Object> formData) {
        // 验证token
        String token = (String) formData.get("token");
        if (!validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("无效的token");
        }

        log.info("接收到表单数据：" + formData);

        // 获取superBO对象
        Object superBOObj = formData.get("superBO");
        ObjectMapper objectMapper = new ObjectMapper();
        testSuperNodeActionBO superBO = null;

        try {
            // 将 LinkedHashMap 转换为 testSuperNodeActionBO 对象
            if (superBOObj instanceof Map) {
                superBO = objectMapper.convertValue(superBOObj, testSuperNodeActionBO.class);
            } else if (superBOObj instanceof String) {
                superBO = objectMapper.readValue((String) superBOObj, testSuperNodeActionBO.class);
            } else {
                throw new IllegalArgumentException("无法转换 superBO 对象");
            }

            // 处理数据
            handleFormData(superBO);

            // 返回成功响应
            return ResponseEntity.ok("数据接收成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("数据转换错误");
        }
    }

    @PostMapping("/approve")
    public ResponseEntity<String> approve(@RequestBody testSuperNodeActionBO formData) {
        // 处理审批通过逻辑
        log.info("审批通过：" + formData);
        return ResponseEntity.ok("审批通过成功");
    }

    @PostMapping("/reject")
    public ResponseEntity<String> reject(@RequestBody testSuperNodeActionBO formData) {
        // 处理驳回逻辑
        log.info("驳回：" + formData);
        return ResponseEntity.ok("驳回成功");
    }

    @PostMapping("/pending")
    public ResponseEntity<String> pending(@RequestBody testSuperNodeActionBO formData) {
        // 处理审批等待逻辑
        log.info("审批等待：" + formData);
        return ResponseEntity.ok("审批等待成功");
    }

    private boolean validateToken(String token) {
        // 实现token验证逻辑
        return true; // 示例中直接返回true
    }

    private void handleFormData(testSuperNodeActionBO superBO) {
        // 处理数据，例如保存到数据库
        // 并将数据传递给前端
        log.info("处理数据：" + superBO);
    }
}

